//
//  DialogShowChooseCheckView.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2019/3/5.
//  Copyright © 2019年 sunke. All rights reserved.
//

#import "DialogShowChooseCheckView.h"
#import "DialogChooseCheckCell.h"

@implementation DialogShowChooseCheckView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = translucentBg;
        _uiView = [[UIView alloc] init];
        _uiView.layer.cornerRadius = 10;
        _uiView.layer.masksToBounds = YES;
        _uiView.backgroundColor = [UIColor whiteColor];
        
        _tvTitle = [[UILabel alloc] init];
        _tvTitle.textColor = title_color;
        _tvTitle.font = textFont;
        _tvTitle.textAlignment = NSTextAlignmentCenter;
        
        _tableChooseView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        [_tableChooseView registerClass:[DialogChooseCheckCell class] forCellReuseIdentifier:@"cellID"];
        _tableChooseView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableChooseView.bounces = NO;
        _tableChooseView.dataSource = self;
        _tableChooseView.delegate = self;
        
        _btnSure = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btnSure.backgroundColor = themeColor;
        _btnSure.tintColor = [UIColor whiteColor];
        [_btnSure setTitle:@"确认" forState:UIControlStateNormal];
        _btnSure.layer.cornerRadius = 5;
        [_btnSure addTarget:self action:@selector(doSure) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self addSubview:_uiView];
        [_uiView addSubview:_tvTitle];
        [_uiView addSubview:_tableChooseView];
        [_uiView addSubview:_btnSure];
        
    }
    return self;
}

-(void)doSure{
    [self removeFromSuperview];
    if(_didSelect){
        for(BaseCheckObject * object in _dataArray){
            if(object.hasSelect){
                _didSelect(object);
                break;
            }
        }
    }
}

-(instancetype)initWith:(CGSize)frame title :(NSString *)tittle withArray : (NSArray *) array andType : (int) type{
    self = [super init];
    _tvTitle.text = tittle;
    _dataArray = array;
    _type = type;
    [_uiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(frame);
    }];
    
    [_tvTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_uiView);
        make.left.right.equalTo(_uiView);
        make.height.mas_equalTo(@50);
    }];

    
    [_btnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_uiView).offset(-15);
        make.centerX.equalTo(_uiView);
        make.size.mas_equalTo(CGSizeMake(150, 40));
    }];
    
    [_tableChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_uiView);
        make.top.equalTo(_tvTitle.mas_bottom).offset(-15);
        make.bottom.equalTo(_btnSure.mas_top);
    }];
    
    [_tableChooseView reloadData];
    return self;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self removeFromSuperview];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DialogChooseCheckCell * cell = [_tableChooseView dequeueReusableCellWithIdentifier:@"cellID"];
    BaseCheckObject * orgEntity = [_dataArray objectAtIndex:indexPath.row];
    cell.tvName.text = [orgEntity valueForKey:_propertyName];
    if(orgEntity.hasSelect){
        cell.stateView.image = [UIImage imageNamed:@"project_select"];
    }else{
        cell.stateView.image = [UIImage imageNamed:@"project_unselect"];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 49;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseCheckObject * object = [_dataArray objectAtIndex:indexPath.row];
    if(object.hasSelect){
        object.hasSelect = NO;
    }else{
        for(int i = 0 ; i < _dataArray.count ;i++){
            if(i == indexPath.row){
                [_dataArray objectAtIndex:i].hasSelect = YES;
            }else{
                [_dataArray objectAtIndex:i].hasSelect = NO;
            }
        }
    }
    [_tableChooseView reloadData];
}


@end
